package t10086.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import t10086.Model.Entity.Order;
import t10086.Repository.OrderRepository;

import java.util.List;

@Service
public class OrderService {
  private final OrderRepository orderRepository;
  @Autowired
  public OrderService(OrderRepository orderRepository) {
      this.orderRepository = orderRepository;
  }

  public List<Order> getOrdersPaged(int pageNo, int pageSize) {
      return orderRepository.findAll(PageRequest.of(pageNo, pageSize)).getContent();
  }
  public  void setOrderStatus(String orderNumber, int status) {
      orderRepository.updateStatusByOrderNumber(orderNumber,status);
  }
  public List<Order> getOrders() {
      return orderRepository.findAll();
  }
  public List<Order> getOrderByStatus(int status) {
      return orderRepository.findOrdersByStatus(status);
  }
  public void  saveOrder(Order order) {
      orderRepository.save(order);
  }
  public Boolean addOrder(Order order) {
      try {
          orderRepository.save(order);
          return true;  // 如果保存成功，返回 true
      } catch (Exception e) {
          // 处理异常
          System.out.println(e.getMessage());  // 打印异常堆栈信息（可选）
          return false;  // 如果保存失败，返回 false
      }
  }
}
